package com.mallcloud.mall.mall.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mallcloud.mall.common.core.util.R;
import com.mallcloud.mall.common.excel.annotation.ResponseExcel;
import com.mallcloud.mall.mall.api.entity.MallOrderModel;
import com.mallcloud.mall.mall.service.MallOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "mall-订单")
@RestController
@RequiredArgsConstructor
@RequestMapping("/mall/order")
public class MallOrderController {
    private final MallOrderService orderService;

    @ApiOperation("分页")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('mall_order_view')")
    public R<IPage<MallOrderModel>> page(Page page, MallOrderModel orderModel) {
        return R.ok(orderService.page(page, Wrappers.lambdaQuery(orderModel).orderByDesc(MallOrderModel::getCreateTime)));
    }

    @ApiOperation("列表")
    @GetMapping("/list")
    @PreAuthorize("@pms.hasPermission('mall_order_view')")
    public R<List<MallOrderModel>> list(MallOrderModel orderModel) {
        return R.ok(orderService.list(Wrappers.lambdaQuery(orderModel).orderByDesc(MallOrderModel::getCreateTime)));
    }

    @ApiOperation("查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('mall_order_view')")
    public R<MallOrderModel> getById(@PathVariable("id") String id) {
        return R.ok(orderService.getById(id));
    }

    @ApiOperation("新增")
	@PostMapping("/save")
    @PreAuthorize("@pms.hasPermission('mall_order_add')")
    public R<Boolean> save(@RequestBody MallOrderModel orderModel) {
		return R.ok(orderService.save(orderModel));
    }

    @ApiOperation("修改")
	@PostMapping("/update")
    @PreAuthorize("@pms.hasPermission('mall_order_edit')")
    public R<Boolean> updateById(@RequestBody MallOrderModel orderModel) {
        return R.ok(orderService.updateById(orderModel));
    }

    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('mall_order_del')")
    public R<Boolean> removeById(@PathVariable Long id) {
        return R.ok(orderService.removeById(id));
    }

	@ApiOperation("导出")
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('mall_order_export')")
    public List<MallOrderModel> export(MallOrderModel orderModel) {
        return orderService.list(Wrappers.query(orderModel));
    }
}
